/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.mushroom.website.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.controller.StripesConstants;
import net.sourceforge.stripes.exception.DefaultExceptionHandler;
import net.sourceforge.stripes.validation.LocalizableError;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.orm.jpa.JpaSystemException;

/**
 * Exception handler for handling some exceptions from business logic
 * 
 * @author Roman Jakubco <roman.jakubco@gmail.com>
 */
public class MyExceptionHandler extends DefaultExceptionHandler{
    public void handleDatabaseException(JpaSystemException exc, HttpServletRequest request, HttpServletResponse response) throws Exception{        
        ActionBean bean = (ActionBean)
        request.getAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN);         
        if (bean != null) {          
            
           if(bean.getContext().getSourcePage() == null){ 
               if( bean instanceof GathererActionBean){
                   LocalizableError err = null;
                   if(exc.getRootCause().getMessage().contains("duplicate")){
                       err = new LocalizableError("error.gatherer.editDuplicate");
                       bean.getContext().getValidationErrors().addGlobalError(err);
                       request.getRequestDispatcher("/protected/editGatherer.jsp").forward(request, response);
                       return;
                   }   else{
                       err = new LocalizableError("admin.delete");
                       bean.getContext().getValidationErrors().addGlobalError(err);
                       request.getRequestDispatcher("/showGatherers.jsp").forward(request, response);
                       return;
                   }
                   
                   
                   
               }
               if( bean instanceof MushroomActionBean){
                   LocalizableError err = null;  
                   if(exc.getRootCause().getMessage().contains("DELETE")){
                       err = new LocalizableError("error.mushroom.delete");
                   }
                   if(exc.getRootCause().getMessage().contains("duplicate")){
                       err = new LocalizableError("error.mushroom.editDuplicate");
                   }
                   bean.getContext().getValidationErrors().addGlobalError(err);
                  request.getRequestDispatcher("/mushroomCatalog.jsp").forward(request, response);
                  return;
               }
               if( bean instanceof ForestActionBean){
                   LocalizableError err = null;
                    if(exc.getRootCause().getMessage().contains("DELETE")){
                       err = new LocalizableError("error.forest.delete");
                   }
                   if(exc.getRootCause().getMessage().contains("duplicate")){
                       err = new LocalizableError("error.forest.editDuplicate");
                   }                  
                   
                   bean.getContext().getValidationErrors().addGlobalError(
                    err);
                   request.getRequestDispatcher("/listForests.jsp").forward(request, response);
                   return;
               }               
                
           } else{
               LocalizableError err = null;  
               if( bean instanceof MushroomActionBean){                                    
                   err = new LocalizableError("error.mushroom.duplicate");
               }
               if( bean instanceof ForestActionBean){
                   err = new LocalizableError("error.forest.duplicate");
               }
               if(bean instanceof GathererActionBean){
                   err = new LocalizableError("error.gatherer.duplicate");
               }              
               
              bean.getContext().getValidationErrors().addGlobalError(err);
              bean.getContext().getSourcePageResolution().execute(request, response); 
           }
            
            
         }
        
    }
    
    public void handleInvalidDataAccessException(InvalidDataAccessApiUsageException ex,
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        ActionBean bean = (ActionBean)
        request.getAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN);
        System.out.println(request.toString());
        if (bean != null) {
            bean.getContext().getValidationErrors().addGlobalError(
                    new LocalizableError("error.mushroom.wrongMonth"));
            
           if(bean.getContext().getSourcePage() == null){
               request.getRequestDispatcher("/mushroomCatalog.jsp").forward(request, response);
           } else{
              bean.getContext().getSourcePageResolution().execute(request, response); 
           }            
            
         }
    }
    
    public void handleAuthorizationException(AuthorizationException ex,
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        ActionBean bean = (ActionBean)
        request.getAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN);
        System.out.println(request.toString());
        if (bean != null) {
            bean.getContext().getValidationErrors().addGlobalError(
                    new LocalizableError("error.auth.role"));
            
           if(bean.getContext().getSourcePage() == null){
               request.getRequestDispatcher("/listForests.jsp").forward(request, response);
           } else{
              bean.getContext().getSourcePageResolution().execute(request, response); 
           }            
            
         }
    }
    
    public void handleUnknownAccountException(UnknownAccountException ex,
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        ActionBean bean = (ActionBean)
        request.getAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN);
        System.out.println(request.toString());
        if (bean != null) {
            bean.getContext().getValidationErrors().addGlobalError(
                    new LocalizableError("error.login"));
            
           if(bean.getContext().getSourcePage() == null){
               request.getRequestDispatcher("/login.jsp").forward(request, response);
           } else{
              bean.getContext().getSourcePageResolution().execute(request, response); 
           }            
            
         }
    }
    public void handleIncorrectCredentialException(IncorrectCredentialsException ex,
            HttpServletRequest request, HttpServletResponse response) throws Exception{
        ActionBean bean = (ActionBean)
        request.getAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN);
        System.out.println(request.toString());
        if (bean != null) {
            bean.getContext().getValidationErrors().addGlobalError(
                    new LocalizableError("error.login"));
            
           if(bean.getContext().getSourcePage() == null){
               request.getRequestDispatcher("/login.jsp").forward(request, response);
           } else{
              bean.getContext().getSourcePageResolution().execute(request, response); 
           }            
            
         }
    }
    
    public void handleWrongGatherer(NumberFormatException ex,HttpServletRequest request, HttpServletResponse response) throws Exception{
        ActionBean bean = (ActionBean)
        request.getAttribute(StripesConstants.REQ_ATTR_ACTION_BEAN);
        System.out.println(request.toString());
        if (bean != null) {
            bean.getContext().getValidationErrors().addGlobalError(
                    new LocalizableError("error.gatherer.editEmpty"));
            
           if(bean.getContext().getSourcePage() == null){
               request.getRequestDispatcher("/showGatherers.jsp").forward(request, response);
           } else{
              bean.getContext().getSourcePageResolution().execute(request, response); 
           }            
            
         }
    }

   
    
}
